package yxy.game.pm2.command.temple;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TempleMessage;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.resonate.ResonateHandle;

/** 置换神殿 七星祭坛武将回退 */
@MessageType(proto = Command.SevenStarRestore, classtype = TempleMessage.SevenStarRestoreRequest.class)
public class CMD_SevenStarRestore extends Command<TempleMessage.SevenStarRestoreRequest> {

	@Override
	public void execute(MessageSource messageSource, TempleMessage.SevenStarRestoreRequest request) {
		String playerCode = messageSource.getTag();
		String heroCode = request.getHero();
		TempleMessage.SevenStarRestoreResponse.Builder response = TempleMessage.SevenStarRestoreResponse.newBuilder();
		try {

			Hero hero = Hero.get(playerCode, heroCode);

			//共鸣的武将不能七星祭坛回退
			Result resultForResonate = ResonateHandle.heroIsResonateHero(hero, playerCode);
			if(!resultForResonate.equals(Result.Success)){
				send(messageSource, response, resultForResonate);
				return;
			}

			Result result = new HeroHandle().sevenStarRestore(playerCode, heroCode, response);
			send(messageSource, response, result);

			//判断如果消耗的是共鸣系统场上的武将，对应操作
			if(result.equals(Result.Success)) {
				ResonateHandle.heroIsAboveHeroAndResolve(hero, playerCode);
			}

		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, TempleMessage.SevenStarRestoreResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}